package one.bugu.android.demon.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class StatusBarUtils {
    /**
     * 单例对象
     */
    private static volatile StatusBarUtils singleton = null;

    public static final String SYSTEM_UI_TRANS = "#00000000";

    public static final String SYSTEM_UI_DARK = "#FFFFFF";

    private StatusBarUtils() {

    }

    /**
     * 获取单例对象
     *
     * @return 单例对象
     */
    public static StatusBarUtils getInstance() {
        if (singleton == null) {
            synchronized (StatusBarUtils.class) {
                if (singleton == null) {
                    singleton = new StatusBarUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public int getStatusBareigt(Context context) {
        int stateBarHeight = 0;
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            stateBarHeight = context.getResources().getDimensionPixelOffset(identifier);
        }
        return stateBarHeight;
    }


    /**
     * 设置状态栏  使用透明颜色
     */
    public void setStatusBar(Activity activity, ViewGroup group) {
        setStatusBar(SYSTEM_UI_TRANS, activity, group, true);
    }

    /**
     * 设置状态栏  +颜色
     * <p>
     * SDK19：可以设置状态栏透明，但是会导致状态栏字无法看见；
     * SDK21：可以设置状态栏颜色，但是不能设置状态栏字体颜色（默认的白色字体在浅色背景下看不清楚）；
     * SDK23(Android 6.0)：可以设置状态栏为浅色（SYSTEM_UI_FLAG_LIGHT_STATUS_BAR），字体就回反转为黑色。
     *
     * @param colorRes    状态栏颜色
     * @param group       设置沉浸式状态栏后页面布局上升, 该group需要动态设置marginytop
     * @param isLightTheme android 6.0后可以设置状态栏文字
     */
    public void setStatusBar(String colorRes, Activity activity, ViewGroup group, boolean isLightTheme) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View view = activity.getWindow().getDecorView();
            int option;
            if (isLightTheme) {
                option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            } else {
                option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            view.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.parseColor(colorRes));
            //设置页面layout高度
            setImmerseStatusBar(activity, group);
        }
    }

    /**
     * 设置状态栏之后整体页面上移, 所以要设置layout marginTop = "状态栏高度"
     *
     * @param group
     */
    public void setImmerseStatusBar(Activity activity, ViewGroup group) {
        //Android5.0以上系统支持设置状态栏
        if (group == null) {
            return;
        }
        try{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(group.getLayoutParams());
                layoutParams.setMargins(0, StatusBarUtils.getInstance().getStatusBareigt(activity), 0, 0);
                group.setLayoutParams(layoutParams);
            }
        }catch (Exception e){
            Log.e("ERROR, result === ", e.getMessage() );
        }

    }

    /**
     * 设置activity嵌套fragment时Statusbar沉浸式
     *
     * @param view 占位view
     */
    public void setFragmentStatusBar(Context context, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = getStatusBareigt(context);
            view.setLayoutParams(layoutParams);
        } else {
            view.setVisibility(View.GONE);
        }
    }
}
